const express = require('express');
const profileRouter = express.Router();

const DB = require('../DataConn/DB');
const { Db } = require('mongodb');

profileRouter.use((req, res, next) => {
    //console.log(new Date(Date.now()), `请求路由/home及其子路由`);
    next();
  })

//检查用户名是否已存在
profileRouter.get('/checkname',(req,res,next)=>{
  let name = req.query.username;
  DB.findDocumentByName(name,(result)=>{
   res.json({
     status:200,
     flag:result.length
   })
  })
})


//注册 向数据库中添加记录
profileRouter.get('/register',(req,res,next)=>{
    let userInfo = {name:req.query.username,password:req.query.password};

    DB.insertDocuments('users',userInfo,callback=>{
      res.send('注册成功!');
    })
});

//登录验证
profileRouter.get('/login',(req,res,next)=>{
  let name = req.query.username;
  let password = req.query.password;
  
  DB.findDocmentByInfo(name,password,result=>{
    res.json({
      status:200,
      flag:result.length
    })
  })
})


module.exports =profileRouter;